package com.cc.sanjieduan.utils;

import com.cc.sanjieduan.pojo.BaseUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.HashMap;
import java.util.Map;

public class SecurityUtils {

    public static Map<Thread, Object> map = new HashMap<>();

    public static BaseUser getUser() {
        //内部使用的是ThreadLocal,在相同的线程的不通的类之间共享数据,ThreadLocal相当于一个map, key是当前线程对象
//        User user = (User) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
//                .getSession().getAttribute("user");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        BaseUser baseUser = (BaseUser) authentication.getPrincipal();
        return baseUser;
    }
}
